import { RandUtil } from "../../util/RandUtil";
import BaseAI, { IBaseAI } from "./BaseAI";

const ru = RandUtil

export interface IAIAwayWall extends IBaseAI {
    checkDis: number | [number, number]
    /** 躲避时偏移角度 默认:0 */
    offsetAng?: number | [number, number]
}

export default class AIAwayWall extends BaseAI {

    private _checkDisArr: number[]
    private _offsetAngArr: number[]
    private _offsetAng: number = 0


    constructor(obj: IAIAwayWall) {
        super(obj)
        this._checkDisArr = this.toMinMax(obj.checkDis)
        this._offsetAngArr = this.toMinMax(obj.offsetAng || 0)
    }

    getKey(): string { return "ai_away_wall" }

    aiStart() {
        let head = this.head
        let checkDis = ru.rand2(this._checkDisArr) * head.w + head.w;
        let checkCol = head.cp().setW(checkDis);
        if (this.world.collideWall(checkCol)) {
            this._offsetAng = (ru.chance(0.5) ? 1 : -1) * ru.rand2(this._offsetAngArr)
        } else {
            this.recheck()
        }
    }

    ai(dt: number): void {
        let head = this.head
        //朝着世界中心 等于避开墙
        head.toAng = head.angTo(this.world.base) + this._offsetAng;
    }

    aiStop() {
    }

}